﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using MapEditor;

namespace PFAGame
{
    public class Camera
    {
        Vector2 position = new Vector2(0, 0);
        public Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;

                // fix la camera sur les limites du tile engine
                if (GameplayScreen.Singleton != null)
                {
                    if (position.X - offsetMiddleScreen.X / ZoomLevel < 0)
                        position.X = offsetMiddleScreen.X / ZoomLevel;
                    if (position.Y - offsetMiddleScreen.Y / ZoomLevel < 0)
                        position.Y = offsetMiddleScreen.Y / ZoomLevel;

                    Vector2 size = GameplayScreen.Singleton.MapP.Size;
                    if (position.X + offsetMiddleScreen.X / ZoomLevel > size.X - 0.5f)
                        position.X = size.X - offsetMiddleScreen.X / ZoomLevel - 0.5f;
                    if (position.Y + offsetMiddleScreen.Y / ZoomLevel > size.Y)
                        position.Y = size.Y - offsetMiddleScreen.Y / ZoomLevel;
                }
            }
        }

        int zoomLevel = 64;
        int halfZoomLevel = 32;
        public int ZoomLevel { get { return zoomLevel; } set { zoomLevel = value; halfZoomLevel = value / 2; } }
        public int HalfZoomLevel { get { return halfZoomLevel; } }

        Vector2 offsetMiddleScreen = new Vector2(ScreenManager.Singleton.GraphicsDevice.Viewport.Width / 2, ScreenManager.Singleton.GraphicsDevice.Viewport.Height / 2);
        public Vector2 OffsetMiddleScreen { get { return offsetMiddleScreen; } }

        Vector2I screenSize = new Vector2I(ScreenManager.Singleton.GraphicsDevice.Viewport.Width, ScreenManager.Singleton.GraphicsDevice.Viewport.Height);
        public Vector2I ScreenSize { get { return screenSize; } }

        /// <summary>
        /// retourne true si la positione st à l'intérieur du frustum de la camera
        /// </summary>
        /// <param name="objectPosition">position testé</param>
        /// <returns></returns>
        public bool isInFrustum(Vector2 objectPosition)
        {
            if (objectPosition.X >= position.X - offsetMiddleScreen.X / zoomLevel - 1 && objectPosition.X <= position.X + offsetMiddleScreen.X / zoomLevel + 1 &&
                objectPosition.Y >= position.Y - offsetMiddleScreen.Y / zoomLevel - 1 && objectPosition.Y <= position.Y + offsetMiddleScreen.Y / zoomLevel + 1.25f)
                return true;
            return false;
        }
    }
}
